﻿Public Class MaterialArgHandler
    Implements IArgHandler

    Private Shared mBuiltInMaterials As HashSet(Of String)
    Private Shared mCreatureItems As HashSet(Of String)

    Shared Sub New()
        mBuiltInMaterials = New HashSet(Of String)
        mBuiltInMaterials.Add("AMBER")
        mBuiltInMaterials.Add("CORAL")
        mBuiltInMaterials.Add("GLASS_GREEN")
        mBuiltInMaterials.Add("GLASS_CLEAR")
        mBuiltInMaterials.Add("GLASS_CRYSTAL")
        mBuiltInMaterials.Add("WATER")
        mBuiltInMaterials.Add("POTASH")
        mBuiltInMaterials.Add("ASH")
        mBuiltInMaterials.Add("PEARLASH")
        mBuiltInMaterials.Add("LYE")
        mBuiltInMaterials.Add("MUD")
        mBuiltInMaterials.Add("VOMIT")
        mBuiltInMaterials.Add("SALT")
        mBuiltInMaterials.Add("FILTH_B")
        mBuiltInMaterials.Add("FILTH_Y")
        mBuiltInMaterials.Add("UNKNOWN_SUBSTANCE")
        mBuiltInMaterials.Add("GRIME")
        mBuiltInMaterials.Add("TRAPPARTS")
        'mBuiltInMaterials.Add("INORGANIC")
        mBuiltInMaterials.Add("COAL")

        'List of items that cause the material to be a creature:caste format
        mCreatureItems = New HashSet(Of String)()
        mCreatureItems.Add("REMAINS")
        mCreatureItems.Add("FISH")
        mCreatureItems.Add("FISH_RAW")
        mCreatureItems.Add("VERMIN")
        mCreatureItems.Add("PET")
        mCreatureItems.Add("EGG")
    End Sub

    Public Shared ReadOnly Property BuiltInMaterials As HashSet(Of String)
        Get
            Return mBuiltInMaterials
        End Get
    End Property

    Public Shared ReadOnly Property CreatureItems As HashSet(Of String)
        Get
            Return mCreatureItems
        End Get
    End Property

    Public ReadOnly Property DefaultTypeData As String Implements IArgHandler.DefaultTypeData
        Get
            Return ""
        End Get
    End Property

    Public ReadOnly Property DefaultValue(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition) As String Implements IArgHandler.DefaultValue
        Get
            Return ""
        End Get
    End Property

    Public Function GetValues(obj As RawObject, argument As ArgumentDefinition) As System.Collections.Generic.IEnumerable(Of String) Implements IArgHandler.GetValues
        Return Nothing
    End Function

    Public Function Parse(argParts As System.Collections.Generic.IEnumerator(Of String), arg As ArgumentDefinition) As String Implements IArgHandler.Parse
        Dim parts As New List(Of String)
        Dim consume As Integer = 1 'The number of extra argument parts to consume

        If mBuiltInMaterials.Contains(argParts.Current) Then
            consume = 1 'the built in materials have 2 arguments, with the 2nd being "none" (usually)
        Else
            Select Case argParts.Current
                Case "CREATURE_MAT"
                    consume = 2
                Case "LOCAL_CREATURE_MAT"
                    consume = 1
                Case "PLANT_MAT"
                    consume = 2
                Case "LOCAL_PLANT_MAT"
                    consume = 1
                Case "GET_MATERIAL_FROM_REAGENT"
                    consume = 2
                Case "NONE"
                    consume = 1
            End Select
        End If

        parts.Add(argParts.Current)
        For i As Integer = 1 To consume
            If argParts.MoveNext Then
                parts.Add(argParts.Current)
            End If
        Next

        Return String.Join(":"c, parts)
    End Function

    Public ReadOnly Property TypeDataFormat As String Implements IArgHandler.TypeDataFormat
        Get
            Return ""
        End Get
    End Property

    Public Function ValidateArgument(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition, value As String) As String Implements IArgHandler.ValidateArgument
        Return Nothing
    End Function

    Public Function ValidateTypeData(typeData As String) As Boolean Implements IArgHandler.ValidateTypeData
        Return True
    End Function

    Public Function GetNavList(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition, value As String) As IEnumerable(Of NavPoint) Implements IArgHandler.GetNavList
        Dim ret As New List(Of NavPoint)
        Dim parts() As String = value.Split(":"c)
        If parts.Count >= 2 Then
            Select Case parts(0)
                Case "CREATURE_MAT", "PLANT_MAT"
                    ret.Add(New NavPoint(parts(1), parts(1), ObjectType.CREATURE))
                Case "LOCAL_CREATURE_MAT", "LOCAL_PLANT_MAT"
                    ret.Add(New NavPoint("USE_MATERIAL_TEMPLATE:" & parts(1) & " on " & obj.ObjectId, obj.ObjectId & ":USE_MATERIAL_TEMPLATE:0:" & parts(1), obj.ObjectType))
                Case "INORGANIC", "METAL", "STONE"
                    ret.Add(New NavPoint(parts(1), parts(1), ObjectType.INORGANIC))
                Case "GET_MATERIAL_FROM_REAGENT"
                    ret.Add(New NavPoint("REAGENT:" & parts(1) & " on " & obj.ObjectId, obj.ObjectId & ":REAGENT:0:" & parts(1), obj.ObjectType))
            End Select
        End If
        Return ret
    End Function
End Class
